package com.css.fxfzfxqh.modules.util.repository.impl;

import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.PCC;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.vo.JcLd;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.vo.JcPga;
import com.css.fxfzfxqh.modules.util.repository.PccRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/06/29
 * @Description: 写入省市县
 */
@Repository
public class PccRepositoryImpl implements PccRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    //抗震能力_正确评估_评估记录主表
    private static final String TABLE_NAME = "fxqh_assess_toponym";

    //根据评估记录id查询地区
    @Override
    public List<PCC> queryToponym(String id) {
        StringBuilder sql = new StringBuilder("select * from fxqh_assess_toponym ");
        StringBuilder whereSql = new StringBuilder(" where assess_id = '"+id+"'");
        return jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(PCC.class));
    }

    //批量保存评估记录的省市县
    @Override
    public void batchSavePCC(List<PCC> pccList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(pccList.toArray());
        String sql = "INSERT INTO fxqh_assess_toponym (uuid,assess_id,id,pid,name,city_name,pp_name,cid,geom)" +
                "values (:uuid,:assessId,:id,:pid,:name,:cityName,:ppName,:cid,st_geomfromtext(:geom,4490))";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveJcLd(List<JcLd> ldData, String AssessId) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(ldData.toArray());
        String sql = "INSERT INTO jc_ld (id,longitude,latitude,ld50year63,ld50year10," +
                "ld50year2,ld100year1,province,province_code,city,city_code,county,county_code,source_flag,del_flag,geom,assess_id,data_version,extends1)" +
                "values (:id,:longitude,:latitude,:ld50year63,:ld50year10," +
                ":ld50year2,:ld100year1,:province,:provinceCode,:city,:cityCode,:county,:countyCode,:sourceFlag,:delFlag,ST_GeometryFromText(:geom,4490),'"+AssessId+"',:dataVersion,:extends1)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveJcPag(List<JcPga> pgaData, String AssessId) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(pgaData.toArray());
        String sql = "INSERT INTO jc_pga (id,longitude,latitude,pga50year63,pga50year10," +
                "pga50year2,pga100year1,province,province_code,city,city_code,county,county_code,source_flag,del_flag,geom,assess_id,data_version,extends1)" +
                "values (:id,:longitude,:latitude,:pga50year63,:pga50year10," +
                ":pga50year2,:pga100year1,:province,:provinceCode,:city,:cityCode,:county,:countyCode,:sourceFlag,:delFlag,ST_GeometryFromText(:geom,4490),'"+AssessId+"',:dataVersion,:extends1)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public Integer delete(String AssessId){
        return jdbcTemplate.update("delete from fxqh_assess_toponym where assess_id='"+AssessId+"'");
    }
}
